package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;


/**
 * @author cxt
 * &#064;Description 自定义Converter[实现从String到ItemType(枚举类型的)的转换]
 * &#064;create 2025/3/15 下午4:24
 */
@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String source) {
        ItemType[] values = ItemType.values();
        for (ItemType itemType : values) {
            if (itemType.getCode().equals(Integer.valueOf(source))) {
                return itemType;
            }
        }
        throw new IllegalArgumentException(
                String.format("type [%s] 错误",source)
        );
    }
}
